///
/// Author: Greg Sandstrom
/// Email: greg.sandstrom@gmail.com
/// Class: CSCI-399
/// Date: 03/24/2008
/// Lab: 4 (Temperature Sensors)
/// IDE: ZDS II - ZNEO 4.11.1 (Build 08081801)
/// Description: Waits for keypress on serial port, and spits out temp from sensors
///
///   Serial Port at 9600
///
///   TMP04
//            [#######]
///           |   |   |
///          PA0 +5V GND
///            
///   MAX7700 
///            +------------[###]------------+
///            |   |   |   |     |   |   |   |
///           GND GND GND +5V   PA7 PA6 PUR GND
///
/// Problems: I tried and tried and tried, but I could never get the analog input to actually read anything but 0
///  Even going back to the "sample" code that Zilog provided, I was still unable to get it working.
///  Figuring out how to get the second byte of the I2C temp was a little difficult, but eventually I got the acks in the right places.
///  PWM was painless.


#include <zneo.h>
#include <sio.h>
#include <stdio.h>

#include "clock.h"
#include "temp_timer.h"
#include "i2c.h"
#include "max7500.h"


#define READ_ZERO 0x01
#define READ_ONE  0x02
unsigned char volatile state = READ_ZERO;
unsigned short volatile highs = 0;
unsigned short volatile lows = 0;
unsigned short volatile counter = 0;

void interrupt isr_timer0( void ) {
	unsigned char x;

	SET_VECTOR(TIMER0, isr_timer0);

	x = (PAIN & 0x01);
	if (state == READ_ZERO) {
		if (x==0) {
			++counter;
		} else {
			state = READ_ONE;
			lows = counter;
			counter = 1;
		}
	} else {
		if (x==1) {
			++counter;
		} else {
			state = READ_ZERO;
			highs = counter;
			counter = 1;
		}
	}
}

void init_pwm_read( void ) {
	PADD |= 0x01; // set PA[0] to input
}

float tmp04_read_c(void) {
	unsigned short h, l;
	h = highs;
	l = lows;

	return 235.0-(400.0*h/l);
}

float tmp04_read_f(void) {
	unsigned short h, l;
	h = highs;
	l = lows;

	return 455.0-(720.0*h/l);
}

void print_pwm() {
	//unsigned short h, l, c;
	//h = highs;
	//l = lows;
	//c = counter;

	printf("PWM-(TMP04)\n");
	//printf("state: %d\n", state);
	//printf("count: %d\n", c);
	//
	//printf("highs: %d\n", h);
	//printf("lows:  %d\n", l);
	//
	printf("c: %f\n", tmp04_read_c());
	printf("f: %f\n", tmp04_read_f());
	printf("-----------\n");
}

void print_i2c() {
	printf("i2c-(MAX7500)\n");
	printf("c: %f\n", max7500_read_temp_c());
	printf("f: %f\n", max7500_read_temp_f());
	printf("-----------\n");
}

unsigned char volatile ping = 0x00;

void interrupt isr_timer1( void ) {
	SET_VECTOR(TIMER1, isr_timer1);
	ping = 1;
}

void main(void) { 

	InitSystemClock();

	init_pwm_read();
	init_timer0();
	init_timer1();
	i2c_init();

    init_uart(_UART0, INTERNAL_CLK_FREQ, 9600); 
	printf("Temperature:\n");


	EI();
	//

	//float temp = max7500_read_temp_f();

    while(1) { 
    	if (ping > 0 || getchar()) {
			ping = 0;
			print_pwm();
			print_i2c();
		}
    }
}